package com.navinfo.tripanalysis.offline.service.impl;

import com.navinfo.gps.gpsroad.FDNaviLib;
import com.navinfo.gps.gpsroad.Gps2Road;
import com.navinfo.gps.gpsroad.GpsInfo;
import com.navinfo.gps.gpsroad.MatchRoad;
import com.navinfo.tripanalysis.common.pojo.Point0200;
import com.navinfo.tripanalysis.offline.service.RoadMapService;
import lombok.Data;
import org.apache.spark.api.java.Optional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.stream.Collectors;

import static java.lang.Math.abs;
@Data
public class RoadMapServiceImpl implements RoadMapService {

    /**
     * 地图数据路径
     */
    private String mapUrl;
    /**
     * 经纬度坐标系：1表示WGS84，2表示GCJ02
     */
    private int gpsType;

    @Override
    public synchronized List<MatchRoad> matchRoadInfos(List<Point0200> point0200) {
        List<MatchRoad> roadInfos;
        List<GpsInfo> gpsInfoList = point0200.stream().map(e ->
        {
            GpsInfo g = new GpsInfo();
            g.setLat(String.valueOf(BigDecimal.valueOf(e.getOriginalLat() * Math.pow(10, -6)).setScale(6, RoundingMode.FLOOR)));
            g.setLon(String.valueOf(BigDecimal.valueOf(e.getOriginalLng() * Math.pow(10, -6)).setScale(6, RoundingMode.FLOOR)));
            g.setAngle(Optional.ofNullable(String.valueOf(e.getDirection())).orElse("0"));
            return g;
        }).collect(Collectors.toList());
        roadInfos = Gps2Road.gps2road(mapUrl, FDNaviLib.enSearchDistance.DISTANCE_500.ordinal(),gpsType,gpsInfoList);
        //根据车辆与道路方向判断坡度
        roadInfos.stream().map(e -> {
            if(e.travelDirection == 2&&abs(Double.valueOf(e.angle)) - abs(e.roadAngle*0.1)>90 && abs(Double.valueOf(e.angle)) - abs(e.roadAngle*0.1) < 270){
                e.setSlopeValue(e.getSlopeValue()*-1);
            }
            return e;
        }).collect(Collectors.toList());
        return roadInfos;
    }

}
